﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
//using UFC.Common;
using System.IO;
using Newtonsoft.Json;
using UFC.DBUtility;

namespace SendIndustrialNetwork
{
   public class SendJsonFileMesToMes
    {
        private const string m_TempCommiType = "SendMesToMes";//推送三坐标
        private static readonly string m_FilePath = ConfigurationManager.AppSettings["acceptFilePath"];//文件接收的位置
        private static readonly string m_BackFilePath = ConfigurationManager.AppSettings["backFilePath"];//文件备份的位置

        public void Excute()
        {
            LogHelp.SetConfig();
            try
            {
                SendData();//数据发送          
            }
            catch (Exception ex)
            {
                LogHelp.WriteError("推送给MES任务数据：" + ex.Message, ex);
            }
        }

        public void SendData() 
        {
             var dirs = new DirectoryInfo(m_FilePath);
             foreach (FileInfo fi in dirs.GetFiles())
             {
                 if (fi.Extension.Replace(".", "").ToLower() != "json")
                     continue;
                 var fileName = Path.GetFileName(fi.FullName);
                 var JsonStr = string.Empty;
                 using (StreamReader s = new StreamReader(fi.FullName))
                 {
                     JsonStr = s.ReadToEnd();
                 }
                 string msg = LYIndustryFileExchangeHelper.FileUpload("80", fileName, JsonStr, "");//文件推送给数据交换系统
                 string code = "";
                 if (!string.IsNullOrWhiteSpace(msg))
                 {
                     try
                     {
                         var reMsgObj = JsonConvert.DeserializeObject<Dictionary<string, string>>(msg);
                         code = reMsgObj["code"];
                         LYIndustryFileExchangeHelper.SendSysLogByUDP(code, "mes管理员", fileName, "xml", JsonStr.Length.ToString(), "");
                     }
                     catch (Exception ex)
                     {
                         CacheHelper.SetToken("token_80", string.Empty);
                         LogHelp.WriteOrcale("MES推送MES结果_处理日志" + "@^MES推送MES返回信息Token失效：" + msg + "@^" + fileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^");
                         continue;
                     }
                 }
                 LYIndustryFileExchangeHelper.MoveBackFile(fi, "");//文件推送完毕，移动备份
                 //推送完毕，更新业务数据，写入MES数据库日志
                
                 OracleDAL.InserOtherSystemRecord(new Dictionary<string, string>() { 
                 {"ParentId",""},{"RecordType",m_TempCommiType},
                        {"ContainerID",""},{"ContainerName",""},{"IsSuccess",code=="200"?"0":"1"},
                        {"ProductName",""},{"WorkflowStepName",""},
                        {"SendXml",JsonStr},{"ResXml",msg}
                });
             }
        }

    }
}
